﻿using GalaSoft.MvvmLight;
using HtScrum.Service.Model;
using HtScrum.Service.Data;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System;
using System.Linq;
namespace HtScrum.UI.Catalog.Customer.ViewModel
{
    public class CustomerDataItemViewModel : ViewModelBase
    {
        public CustomerDataItemViewModel(IServiceProvider serviceProvider)
        {
            this.ServiceProvider = serviceProvider;
        }

        #region Public Properties
        private EntityContract<CustomerModel> _EntityModel = null;
        public EntityContract<CustomerModel> EntityModel
        {
            get
            {
                return _EntityModel;
            }

            set
            {
                _EntityModel = value;
                this.Refresh();
            }
        }
        #endregion

        #region Commands
        public ICommand DeleteCommand { get { return new RelayCommand(ExecuteDeleteCommand); } }
        #endregion

        #region Public Methods
        public void ExecuteDeleteCommand()
        {
            CustomerDataCollectionViewModel customerDataCollectionViewModel = this.ServiceProvider.GetService(typeof(CustomerDataCollectionViewModel)) as CustomerDataCollectionViewModel;
            customerDataCollectionViewModel.DataCollection.Where(c => c == this.EntityModel).FirstOrDefault().State = eEntityState.Delete;
            customerDataCollectionViewModel.ViewModelCollection.Remove(this);
        }
        #endregion

        public void Refresh()
        {
            RaisePropertyChanged("EntityModel");
        }

        public IServiceProvider ServiceProvider { get; set; }
    }
}
